//
//  ViewController.m
//  payDemo
//
//  Created by lairen on 15/9/16.
//  Copyright (c) 2015年 lairen. All rights reserved.
//

#import "ViewController.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

@interface ViewController ()
- (IBAction)pay;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


//支付按钮点击的方法
- (IBAction)pay {
    NSString *partner = @"";
    NSString *seller = @"";
    NSString *privateKey = @"";
    //如果partner和seller获取失败,提示用户
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    //2.创建订单对象
    Order *order=[[Order alloc]init];
    order.partner=partner;
    order.seller=seller;
    order.tradeNO=@"201591734927845485340";  //订单ID
    order.productName=@"iphone 6s" ;   //商品标题
    order.productDescription = @"超高配置 2个内存 配A9处理器  其性能是iphone6的1.8倍"; //商品描述
    order.amount = @"0.01" ;//商品价格
    order.notifyURL =  @"http://www.xxx.com"; //回调URL
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
  
    NSString *appScheme = @"thinklion";
    
    //将商品信息拼接成字符串  商品信息也是服务器返回的
    NSString *orderSpec = [order description];
    //此数据是服务器返回的证书类型
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    //此签名信息是Util和openssl里面的文件生成的数据   其实应该是我们服务器端给我们返回的
    NSString *signedString = [signer signString:orderSpec];
    
    NSLog(@"orderSpec = %@",orderSpec);
    
   NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                   orderSpec, signedString, @"RSA"];
    
    //如果有签名字符串 才打开支付
    if(signedString!=nil){
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
        
    }
    
    
    
}




















@end
